// pages/memberCenter/memberCenter.js
const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      currentTab:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  currentTab:function (e) {
      var that = this;
      var newId = e.currentTarget.dataset.current;
      if (this.data.currentTab == newId) {
          return false;
      } else {
          that.setData({
              currentTab: newId
          })
      }
  },
  onLoad: function (options) {
      var that = this
      that.resetChallengeObj()
      console.log(app.globalData.userInfo)
      that.setData({
          userInfo:app.globalData.userInfo
      })
      that.adver()
  },
    //广告接口
    adver:function () {
        var that = this
        wx.request({
            url: 'https://caige1.sanliwenhua.com/program/column?app=7',
            method: "get",
            success: function (res) {
                //console.log(res, "广告")
                if (res.data.code = 200) {
                    var data = res.data.data;
                    var AdvData = []
                    var prizeData = []
                    for(var i = 0;i<data.length;i++){
                        if(data[i].type=="shop"){
                            prizeData.push(data[i])
                        }
                    }
                    for(var i = 0;i<data.length;i++){
                        if(data[i].type=="advs"){
                            AdvData.push(data[i])
                        }
                    }
                    console.log(AdvData,prizeData)
                    that.setData({
                        avdUrl: AdvData,
                        prizeData:prizeData
                    })
                }
            }
        })
        /*wx.request({
            url: 'https://caige1.sanliwenhua.com/switch',
            method: "get",
            success: function (res) {
                //console.log(res, "开关")
                if (res.data.code = 200) {
                    var isADV = res.data.data.isADV;
                    that.setData({
                        isADV: isADV //广告开关写死
                    })
                } else {

                }
            }
        })*/

    },
    resetChallengeObj:function () {
        var that = this
        var myTime = new Date();
        var today = myTime.getDate();
        wx.getStorage({
            key: 'ChallengeObj',
            success: function (res) {
                var ChallengeObj = JSON.parse(res.data)
                //console.log('ChallengeObj', ChallengeObj)
                if (today != ChallengeObj.day) {
                    ChallengeObj = {
                        day: today,
                        ChallengeTime: 3
                    }
                }
                wx.setStorage({
                    key: 'ChallengeObj',
                    data: JSON.stringify(ChallengeObj)
                })
                app.globalData.ChallengeObj = ChallengeObj
                console.log(app.globalData.ChallengeObj)
                that.setData({
                    ChallengeTime:ChallengeObj.ChallengeTime
                })

            }, fail: function (res) {
                //console.log(res)
                var ChallengeObj = {
                    day: today,
                    ChallengeTime: 3
                }
                wx.setStorage({
                    key: 'ChallengeObj',
                    data: JSON.stringify(ChallengeObj)
                })
                app.globalData.ChallengeObj = ChallengeObj
                that.setData({
                    ChallengeTime:ChallengeObj.ChallengeTime
                })
            }
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
      var that = this;
      var nickeName = that.data.userInfo.nickName;
      if (res.from === 'button') {

      }
      return {
          title: '['+nickeName+'@我],那些年学过的成语，还记得吗？快来挑战吧！',
          path: 'pages/index/index',
          imageUrl: 'http://paz0eiqln.bkt.clouddn.com/image/fenxing2.jpg',
          success: function (res) {
              wx.showToast({
                  title: '分享成功!',
                  icon: 'success',
                  duration: 2000
              })
              console.log('分享到群的标记', res)
              /*if(res.shareTickets){
                  app.globalData.shareTicket = res.shareTickets[0]
                  if(app.globalData.platform == "android"){
                      this.update()
                  }
                  console.log(app.globalData.Sharing)
              }else{
                wx.showModal({
                      title: "提示",
                      content: "分享到群可以看到朋友的排行哦!",
                      showCancel: false,
                      confirmText: "确定"
                  })
              }*/
          },
          fail: function (res) {
              wx.showToast({
                  title: res.data.message,
                  icon: 'success',
                  duration: 2000
              })
              /*wx.showToast({
                  title: '分享失败！',
                  image: '/image/errIcon.png',
                  duration: 2000
              })*/
          }
      }
  
  }
})